/*
 * topological-hex, a program to compute combinatorial hexahedral meshes.
 *
 * Copyright (C) <2018> <Université catholique de Louvain (UCL), Belgique>
 *
 * List of the contributors to the development, description and complete
 * License: see LICENSE file.
 *
 * This program (topological-hex) is free software:
 * you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program (see COPYING file).  If not,
 * see <http://www.gnu.org/licenses/>.
 */


#include <check.h>
#include <stdlib.h>

#include "utils.h"

START_TEST(test_lsb32) {
  for (uint32_t i = 0; i < 1024; i++) {
    uint32_t n = (1u << 31);
    uint64_t min = 31;
    for (uint32_t i = 0; i < 32; i++) {
      uint32_t i = rand() % 32;
      n |= (1ul << i);
      if (i < min) min = i;
      ck_assert_int_eq(find_lsb32(n), min);
    }
  }
} END_TEST

START_TEST(test_lsb64) {
  for (uint32_t i = 0; i < 1024; i++) {
    uint64_t n = (1ul << 63ul);
    uint64_t min = 63;
    for (uint32_t i = 0; i < 64; i++) {
      uint32_t i = rand() % 64;
      n |= (1ul << i);
      if (i < min) min = i;
      ck_assert_int_eq(find_lsb64(n), min);
    }
  }
} END_TEST

Suite *utils_suite(void) {
  Suite *s = suite_create("Utils");

  TCase *tcase = tcase_create("LSB");
  tcase_add_test(tcase, test_lsb32);
  tcase_add_test(tcase, test_lsb64);
  suite_add_tcase(s, tcase);

  return s;
}
